﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using CardEngine.CGService;
using System.Windows.Forms;

namespace CardEngine
{
    public partial class frmPlayer : Form
    {
        public enum PlayArea 
        {
    	    Table, Player
        }

        public frmPlayer()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void picDealer1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void frmPlayer_Load(object sender, EventArgs e)
        {

        }

        public void redraw()
        {
            CardGameServiceClient client = new CardGameServiceClient();
            Dictionary<Player, PlayerCards[]> pcs = client.GetCurrentTableView();
            PlayerCards[] value = null;
            string path = Path.GetDirectoryName(Application.ExecutablePath).Trim();
            foreach (Player key in pcs.Keys)
	        {
                pcs.TryGetValue(key, out value);

                #region "The Primary Player (Player 1)'s area"
                if (key.PlayerName.ToLower().Trim().Equals("player 1"))
                {
                    if (value.Length > 0)
                    {
                        imgPlayer1.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(0)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgPlayer1.Image = Image.FromFile(path + "//CardImages//68.gif");
                    }
                    if (value.Length > 1)
                    {
                        imgPlayer2.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(1)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgPlayer2.Image = Image.FromFile(path + "//CardImages//68.gif");
                    }
                    if (value.Length > 2)
                    {
                        imgPlayer3.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(2)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgPlayer3.Image = Image.FromFile(path + "//CardImages//68.gif");
                    }
                    if (value.Length > 3)
                    {
                        imgPlayer4.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(3)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgPlayer4.Image = Image.FromFile(path + "//CardImages//68.gif");
                    }
                    if (value.Length > 4)
                    {
                        imgPlayer5.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(4)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgPlayer5.Image = Image.FromFile(path + "//CardImages//68.gif");
                    }
                }
                #endregion

                #region "The Table's area"
                if (key.PlayerName.ToLower().Trim().Equals("table"))
                {
                    if (value.Length > 0)
                    {
                        if (imgOpponent1.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate{imgTable1.Visible = true;}));
                            return;
                        }
                        imgTable1.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(0)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgTable1.Image = Image.FromFile(path + "//CardImages//68.gif");
                        if (imgTable1.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate
                            {
                                imgTable1.Visible = false;
                            }));
                            return;
                        }
                    }
                    if (value.Length > 1)
                    {
                        if (imgTable2.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate { imgTable2.Visible = true; }));
                            return;
                        }
                        imgTable2.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(1)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgTable2.Image = Image.FromFile(path + "//CardImages//68.gif");
                        if (imgTable2.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate
                            {
                                imgTable2.Visible = false;
                            }));
                            return;
                        }
                    }
                    if (value.Length > 2)
                    {
                        if (imgTable3.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate { imgTable3.Visible = true; }));
                            return;
                        }
                        imgTable3.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(2)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgTable3.Image = Image.FromFile(path + "//CardImages//68.gif");
                        if (imgTable3.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate
                            {
                                imgTable3.Visible = false;
                            }));
                            return;
                        }
                    }
                    if (value.Length > 3)
                    {
                        if (imgTable4.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate { imgTable4.Visible = true; }));
                            return;
                        }
                        imgTable4.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(3)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgTable4.Image = Image.FromFile(path + "//CardImages//68.gif");
                        if (imgTable4.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate
                            {
                                imgTable4.Visible = false;
                            }));
                            return;
                        }
                    }
                    if (value.Length > 4)
                    {
                        if (imgTable5.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate { imgTable5.Visible = true; }));
                            return;
                        }
                        imgTable5.Image = Image.FromFile(path + "//CardImages//" + ((PlayerCards)value.GetValue(4)).ImageFileName.Trim() + ".gif");
                    }
                    else
                    {
                        imgTable5.Image = Image.FromFile(path + "//CardImages//68.gif");
                        if (imgTable5.InvokeRequired) //<<======Now it wont give exception**
                        {
                            this.Invoke(new MethodInvoker(delegate
                            {
                                imgTable5.Visible = false;
                            }));
                            return;
                        }
                    }
                }
                #endregion
            }
            client.Close();
            client = null;
        }
    }
}
